package com.zzj.ecology.admin.controller;

import com.zzj.ecology.common.config.Global;
import com.zzj.ecology.framework.utils.SecurityUtils;
import com.zzj.ecology.system.dto.HttpResult;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName BaseController
 * @Description TODO
 * @Author zhijun zeng at 2020/3/1
 * @Version 1.0
 **/
@RestController
public class BaseController {

//    public User getPrincipal2(HttpServletRequest request) {
//        // UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication() .getPrincipal();
//        // 上述运用中发现获得的Authentication为null。仔细看了下源代码发现，如果想用上面的代码获得当前用户，必须在spring
//        // security过滤器执行中执行，否则在过滤链执行完时org.springframework.security.web.context.SecurityContextPersistenceFilter类会
//        //调用SecurityContextHolder.clearContext();而把SecurityContextHolder清空，所以会得到null。
//        // 经过spring security认证后，security会把一个SecurityContextImpl对象存储到session中，此对象中有当前用户的各种资料
//
//        SecurityContextImpl securityContextImpl = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
//        // 登录名
//        System.out.println("Username:"
//                + securityContextImpl.getAuthentication().getName());
//        // 登录密码，未加密的
//        System.out.println("Credentials:"
//                + securityContextImpl.getAuthentication().getCredentials());
//        WebAuthenticationDetails details = (WebAuthenticationDetails) securityContextImpl
//                .getAuthentication().getDetails();
//        // 获得访问地址
//        System.out.println("RemoteAddress" + details.getRemoteAddress());
//        // 获得sessionid
//        System.out.println("SessionId" + details.getSessionId());
//        // 获得当前用户所拥有的权限
//        List<GrantedAuthority> authorities = (List<GrantedAuthority>) securityContextImpl
//                .getAuthentication().getAuthorities();
//        for (GrantedAuthority grantedAuthority : authorities) {
//            System.out.println("Authority" + grantedAuthority.getAuthority());
//        }
//        return null;
//    }


    protected HttpResult success() {
        return HttpResult.success();
    }

    protected HttpResult success(HttpResult result) {
        return result;
    }

    protected HttpResult successData(Object object) {
        return success().add(HttpResult.DATA_TAG, object);
    }

    protected HttpResult error() {
        return HttpResult.error();
    }



    /**
     * 获取当前登录用户
     * @return
     */
    protected String getUsername() {
        return SecurityUtils.getUsername();
    }

    protected String getAuthInitializePassword() {
        return SecurityUtils.encode(Global.getInitializePassword());
    }




}
